package v1

import (
	"Im/server/common/model"
	"Im/server/util"

	"github.com/gin-gonic/gin"
)

type Friend struct {
	ApiBaseVerify
}

type FriendList struct {
	UserList model.User
	Fid      uint64
}

// 通讯录(好友列表)
func (s Friend) Friendlist(c *gin.Context) {

	fid := c.DefaultPostForm("fid", "")
	if fid == "" {
		uInfo, _ := s.GetUserInfo(c)
		fid = uInfo.Id
	}
	friendM := new(model.FriendModel)
	where := make(map[string]string)
	where["fid"] = fid
	friArr, _, bol := friendM.ListData(util.StrToUint64(fid), where)
	if bol == true {
		s.Succee(friArr, c)
	} else {
		s.Error(0, "", "无好友", c)
	}
	return
}

// 好友详情资料
func (s Friend) FrienDetail(c *gin.Context) {

	var fid string = c.DefaultPostForm("friend_id", "")
	userM := new(model.UserModel)
	userInfo, _, _ := userM.Detail(util.StrToUint64(fid), nil)
	reData := &FriendList{
		UserList: userInfo,
		Fid:      0,
	}
	s.Succee(reData, c)
	return
}

// 陌生人列表
func (s Friend) Stranger(c *gin.Context) {

	fid := c.DefaultPostForm("fid", "")
	uInfo, _ := s.GetUserInfo(c)
	if fid == "" {
		fid = uInfo.Id
	}
	friendM := new(model.FriendModel)
	where := make(map[string]string)
	where["fid"] = fid
	friArr := friendM.ListStranger(util.StrToUint64(fid), where, uInfo.Uid)
	s.Succee(friArr, c)
	return
}

// 添加好友
func (s Friend) StrangerAdd(c *gin.Context) {
	var stranger_id string = c.DefaultPostForm("stranger_id", "")
	uInfo, _ := s.GetUserInfo(c)

	var friendD = make(map[string]string)
	friendD["stranger_id"] = stranger_id
	friendD["fid"] = uInfo.Uid
	friendM := new(model.FriendModel)

	reErr := friendM.StrangerAdd(friendD)
	if reErr == true {
		s.Succee("成功", c)
	} else {
		s.Error(0, "", "失败", c)
	}
	return
}

// 删除好友
func (s Friend) StrangerDel(c *gin.Context) {
	var fid string = c.DefaultPostForm("fid", "")

	friendM := new(model.FriendModel)
	reErr := friendM.Del(util.StrToUint64(fid))
	if reErr == true {
		s.Succee("成功", c)
	} else {
		s.Error(0, "", "失败", c)
	}
	return
}
